const fs = require('fs')
const path = require('path')

// 判断文件在不在函数
function exists(src, dist) {
  fs.exists(dist, function (exists) {
    if (exists) {
      copy && copy(src, dist)
    } else {
      fs.mkdir(dist, function () {
        copy && copy(src, dist)
      })
    }
  })
}


// 拷贝文件函数
function copy(src, dist) {
  // 第一步，需要知道 src 有什么
  fs.readdir(src, function (err, paths) {
    paths.forEach(item => {
      let _src = src + '/' + item,
        _dist = dist + '/' + item
      // let _src = path.resolve(src, item);
      // _dist = path.resolve(dist, item);
      console.log(_src, '---1')
      console.log(_dist, '---2')
      fs.stat(_src, function (err, stat) {
        if (stat.isFile()) {
          fs.readFile(_src, function (err, data) {
            fs.writeFile(_dist, data, function () {
              console.log('读取成功')
            })
          })
        } else if (stat.isDirectory()) {
          exists(_src, _dist)
        }
      })
    })
  })
}

exists(__dirname + '/src', __dirname + '/dist')